﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLyNhanVien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsTV();
        }

        public void LaydsTV()
        {
            string mtv = "";
            DataTable dt = LietKeTV_Ma(mtv);
            DDLGioitinh.Items.Add("Nam");
            DDLGioitinh.Items.Add("Nữ");
            grThanhVien.DataSource = dt;
            grThanhVien.DataBind();
        }


        public DataTable LietKeTV_Ma(string Matv)
        {
            try
            {
                if (Matv != null || Matv != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@matv", Matv));
                    return CDatastore.DocStore("SP_TIMKIEM_THANHVIEN_MATV", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_THANHVIEN_MATV");
        }

        public DataTable LietKeTen(string ten)
        {
            lblThongBao.Text = "";
            try
            {
                if (ten != null || ten != "")
                {
                    
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@ten", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_THANHVIEN_TEN", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
                lblThongBao.Text = "Không Tìm Thấy";
            }
            return CDatastore.DocStore("SP_TIMKIEM_THANHVIEN_MATV");
        }


        protected void grThanhVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaTV.Text = grThanhVien.SelectedRow.Cells[1].Text;
            txtMatKhau.Text = grThanhVien.SelectedRow.Cells[2].Text;
            txtHoTen.Text = grThanhVien.SelectedRow.Cells[3].Text;
            txtCMND.Text = grThanhVien.SelectedRow.Cells[4].Text;
            txtNgaySinh.Text = grThanhVien.SelectedRow.Cells[5].Text;
            bool isChecked = ((CheckBox)grThanhVien.SelectedRow.Cells[0].FindControl("1")).Checked;
            if (isChecked == true)
                DDLGioitinh.Text = "Nam";
            else
                DDLGioitinh.Text = "Nữ";
            txtDienThoai.Text = grThanhVien.SelectedRow.Cells[7].Text;
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@matv", txtMaTV.Text.ToString()));
                sqlPara.Add(new SqlParameter("@matkhau", txtMatKhau.Text.ToString()));
                sqlPara.Add(new SqlParameter("@hoten", txtHoTen.Text.ToString()));
                sqlPara.Add(new SqlParameter("@cmnd", txtCMND.Text.ToString()));
                sqlPara.Add(new SqlParameter("@ngaysinh", DateTime.Parse(txtNgaySinh.Text.ToString())));
                if(DDLGioitinh.Text=="Nam")
                   sqlPara.Add(new SqlParameter("@gioitinh", "True"));
                else
                   sqlPara.Add(new SqlParameter("@gioitinh", "False"));
                sqlPara.Add(new SqlParameter("@dienthoai", txtDienThoai.Text.ToString()));
                CDatastore.ThucHienStore("PRO_INSERT_THANHVIEN", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Thành Viên thành công";
                txtMaTV.Text=null;
                txtMatKhau.Text = null;
                txtHoTen.Text = null;
                txtNgaySinh.Text = null;
                DDLGioitinh.Items.Clear();
                txtDienThoai.Text = null;
                LaydsTV();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

   

        protected void Button1_Click1(object sender, EventArgs e)
        {
            //string s = "select * from thanhvien where matv='" + txtMaTV.Text + "'";
            //DataTable dt = CDatabase.DocBang(s);
            DataTable dt = LietKeTV_Ma(txtMaTV.Text);
            if (dt.Rows.Count != 0 && txtMaTV.Text!="")
            {
                lblThongBao.Text = "";
                txtMatKhau.Text = dt.Rows[0][1].ToString();
                txtHoTen.Text = dt.Rows[0][2].ToString();
                txtCMND.Text = dt.Rows[0][3].ToString();
                txtNgaySinh.Text = dt.Rows[0][4].ToString();
                if (dt.Rows[0][5].ToString() == "True")
                    DDLGioitinh.Text = "Nam";
                else
                    DDLGioitinh.Text = "Nữ";
                txtDienThoai.Text = dt.Rows[0][6].ToString();
                LaydsTV();
            }
            else
            {
                lblThongBao.Text = "Không tìm thấy thành viên";
                LaydsTV();
            }
        }

        protected void imXoa_Click1(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@matv", txtMaTV.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_THANHVIEN_MATV", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Thành Viên " + txtMaTV.Text + " thành công";
                DDLGioitinh.Items.Clear();
                LaydsTV();
                txtMaTV.Text = null;
                txtMatKhau.Text = null;
                txtHoTen.Text = null;
                txtNgaySinh.Text = null;
                txtDienThoai.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imSua_Click1(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@matv", txtMaTV.Text.ToString()));
                sqlPara.Add(new SqlParameter("@matkhau", txtMatKhau.Text.ToString()));
                sqlPara.Add(new SqlParameter("@hoten", txtHoTen.Text.ToString()));
                sqlPara.Add(new SqlParameter("@cmnd", txtCMND.Text.ToString()));
                sqlPara.Add(new SqlParameter("@ngaysinh", DateTime.Parse(txtNgaySinh.Text.ToString())));
                if (DDLGioitinh.Text == "Nam")
                    sqlPara.Add(new SqlParameter("@gioitinh", "True"));
                else
                    sqlPara.Add(new SqlParameter("@gioitinh", "False"));
                sqlPara.Add(new SqlParameter("@dienthoai", txtDienThoai.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_THANHVIEN_MATV", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Thành Viên " + txtMaTV.Text + " thành công";
                txtMaTV.Text = null;
                txtMatKhau.Text = null;
                txtHoTen.Text = null;
                txtNgaySinh.Text = null;
                DDLGioitinh.Items.Clear();
                txtDienThoai.Text = null;
                LaydsTV();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dt = LietKeTen(txtHoTen.Text);
            grThanhVien.DataSource = dt;
            grThanhVien.DataBind();
            txtMaTV.Text = null;
            txtMatKhau.Text = null;
            txtHoTen.Text = null;
            txtNgaySinh.Text = null;
            txtDienThoai.Text = null;
        }

        protected void grThanhVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}